<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class transfer extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('product/transfer_model');
		$this->load->library('datatables');			
	}
	function get(){
		//echo $this->transfer_model->get();
	}
	
	function trasfer_product()
	{
		$meta['page_title'] = "Transfer Product";
		$this->data['page_title'] = "Create branch";
		$this->data['branches']=$this->transfer_model->get_branch();
		$this->data['products']=$this->transfer_model->get_product();
		$trans_id['id']=$this->transfer_model->get_max_transfer()->row();
		$company['profile']=$this->transfer_model->get_invoice()->row();
		$invoice_code=$company['profile']->invoice_code;
		$id=$this->session->userdata['user_id'];
		
		$user['detail']=$this->transfer_model->get_user($id)->row();
		$branch_id=$user['detail']->company;
		$branch['code']=$this->transfer_model->get_branch_code($branch_id)->row();
		$branch_code=$branch['code']->code;
	    $max_id=$trans_id['id']->id+1;	
	    $transfer_code='TRANS'.'/'.$invoice_code.'/'.$branch_code.'/'.$max_id;
		
		  
		//validate form input
		//$this->form_validation->set_rules('id_branch', 'SOURCE BRANCH', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('destination_branch', 'DESTINATION BRANCH', 'required|xss_clean');
		$this->form_validation->set_rules('source_branch', 'Source Branch', 'required|is_natural|xss_clean');
		//$this->form_validation->set_rules('quantity', 'Quantity', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{ 
		 
		       $source=$this->input->post('source_branch');
			   $branch['source']=$this->transfer_model->get_branch_name($source);
			  $destination= $this->input->post('destination_branch');
			  $branch['destination']=$this->transfer_model->get_branch_name($destination);
			  $data1=array('source_branch'=>$this->input->post('source_branch'),
		               'destination_branch'=>$this->input->post('destination_branch'),
					   'date'             =>$this->input->post('date'),
					   'transfer_no'      =>$this->input->post('transfer_id'),
					   'source_branch_name'=>$branch['source'],
					   'status'            =>'Pending',
					    'destination_branch_name'=>$branch['destination'],
					   );
					   $latest_id=$this->transfer_model->insert_transfer($data1);
	          $item_code=$this->input->post('item_code');
			  $item_name=$this->input->post('item_name');
			  $quantity=$this->input->post('quantity');
			   $length = count($item_code);
				for($i=0; $i<$length; $i++){
                $data['quantity']=$quantity[$i];
				$product['detail']=$this->transfer_model->get_product_detail($item_code[$i])->row();
				$product_id=$product['detail']->id;
				$data['transfer_id']=$latest_id;
				$data['product_code']=$item_code[$i];
				$data['id_product']=$product_id;
				$data['source_branch']=$branch['source'];
				$data['destination_branch']=$branch['destination'];
            $this->transfer_model->insert_transfer_product($data);
			//$this->transfer_model->update_source($source,$product_id,$quantity[$i]);
			//$this->transfer_model->update_destination($destination,$product_id,$quantity[$i]);
			  }
			
			redirect("product/transfer/view_transfer");
			redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['product_code'] = array(
				'name'  => 'product_code',
				'id'    => 'product_code',
				'class' => 'span6',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('product_code'),
			);

			$this->data['source_branch'] = array(
				'name'  => 'source_branch',
				'id'    => 'source_branch',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('source_branch')
			);
	
			$this->data['destination_branch'] = array(
			'name'  => 'destination_branch',
			'id'    => 'destination_branch',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('destination_branch'),
			
			);
			
	        $this->data['transfer_id'] = array(
			'name'  => 'transfer_id',
			'id'    => 'transfer_id',
			'type'  => 'text',
			
			'value' => $this->form_validation->set_value('transfer_id',$transfer_code),
			
			);
		
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date'),
			'class' => 'datepicker'
			);
		
			
		//	print_r($this->data);exit;

			$this->load->view('common/header',$meta);
			$this->load->view('product/transfer/transfer', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	
	function view_transfer(){
	
		//$this->data['transfers'] = $this->transfer_model->get_transfer()->result();
	   $meta['page_title'] = "Transfer Details";
	   $this->data['transfer']= $this->transfer_model->get();
	   $this->load->view('common/header', $meta);
	   $this->load->view('product/transfer/view_transfer1',$this->data);
       $this->load->view('common/footer');
		
	}
		function view_approved_transfer(){
	
		//$this->data['transfers'] = $this->transfer_model->get_transfer()->result();
	   $meta['page_title'] = "Transfer Details";
	   $this->data['transfer']= $this->transfer_model->get_approved();
	   $this->load->view('common/header', $meta);
	   $this->load->view('product/transfer/view_approved_transfer',$this->data);
       $this->load->view('common/footer');
		
	}
		
	function check_branch_product(){
	$code['id']=$this->transfer_model->get_product_id($_REQUEST['code'])->row();
 
		echo $this->transfer_model->check_branch_product($_REQUEST['branch'], $code['id']->id,$_REQUEST['quantity']);exit;
	}		

	function get_destination_branch(){
	$destination_branch=$this->transfer_model->get_destinaion_branch($_REQUEST['branch']);
	echo $destination_branch;exit;
	}

	public function edit_transfer_order($id)
	{ 
	    $this->data['branches']=$this->transfer_model->get_branch();
		$this->data['transfer']=$this->transfer_model->get_transfer_det($id)->row();
		
	    $this->data['id']=$id;
		$this->data['transfer_product']=$this->transfer_model->get_transfer_pdt($id);
	
	    //validate form input
		//$this->form_validation->set_rules('id_branch', 'SOURCE BRANCH', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('destination_branch', 'DESTINATION BRANCH', 'required|xss_clean');
		$this->form_validation->set_rules('source_branch', 'Source Branch', 'required|is_natural|xss_clean');
		//$this->form_validation->set_rules('quantity', 'Quantity', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{  $user=$this->data['transfer'];
	       $previous_product=$this->data['transfer_product'];
		  
		       $source=$this->input->post('source_branch');
			   $branch['source']=$this->transfer_model->get_branch_name($source);
			  $destination= $this->input->post('destination_branch');
			
			  $branch['destination']=$this->transfer_model->get_branch_name($destination);
			  $data1=array('source_branch'=>$this->input->post('source_branch'),
		               'destination_branch'=>$this->input->post('destination_branch'),
					   'date'             =>$_REQUEST['date'],
					   'source_branch_name'=>$branch['source'],
					   'destination_branch_name'=>$branch['destination'],
					 );
					  
		$this->transfer_model->update_transfer($data1,$user->id);
	          $item_code=$_REQUEST['item_code'];
			
			  $item_name=$this->input->post('item_name');
			  $quantity=$this->input->post('quantity');
			   $length = count($item_code);
			
				for($i=0; $i<$length; $i++){
                $data['quantity']=$quantity[$i];
				$product['detail']=$this->transfer_model->get_code_detail($item_code[$i]);
			
				$product_id=$product['detail']->id;
	            
				$data['transfer_id']=$user->id;
				$data['product_code']=$item_code[$i];
				$data['id_product']=$product_id;
				$data['source_branch']=$branch['source'];
				$data['destination_branch']=$branch['destination'];
			 
          $this->transfer_model->update_transfer_product($previous_product,$data,$user,$data1,$id,$product_id);
		  //$this->transfer_model->update_transfer_source($source,$product_id,$quantity[$i],$destination,$user,$previous_product);
			//$this->transfer_model->update_transfer_destination($destination,$product_id,$quantity[$i]);
			  }
			
			redirect("product/transfer/view_transfer");
			redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			

			$this->data['source_branch'] = array(
				'name'  => 'source_branch',
				'id'    => 'source_branch',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('source_branch',$this->data['transfer']->source_branch)
			);
	
			$this->data['destination_branch'] = array(
			'name'  => 'destination_branch',
			'id'    => 'destination_branch',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('destination_branch'),
			
			);
			
	        $this->data['transfer_id'] = array(
			'name'  => 'transfer_id',
			'id'    => 'transfer_id',
			'type'  => 'text',
			'readonly'=>true,
			
			'value' => $this->form_validation->set_value('transfer_id',$this->data['transfer']->transfer_no),
			
			);
		
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date',$this->data['transfer']->date),
			'class' => 'datepicker'
			);
		
			
	
		 $meta['page_title'] = " Edit Transfer Details";
    	$this->load->view('common/header',$meta);
		$this->load->view('product/transfer/transfer_edit', $this->data);
		$this->load->view('common/footer');
	
	}}
	public function view_transfer_order($id)
	{
	$this->data['transfer_details'] =	$this->transfer_model->get_transfer_detail($id)->row();
	
    $this->data['detail']  =	$this->transfer_model->get_transfer_product($id);
	
    $this->load->view('product/transfer/transfer_detail',$this->data);
	
	}
	public function view_approved_order($id)
	{
	$this->data['transfer_details'] =	$this->transfer_model->get_transfer_detail($id)->row();
	
    $this->data['detail']  =	$this->transfer_model->get_transfer_product($id);
	
    $this->load->view('product/transfer/approved_transfer_detail',$this->data);
	
	}
	
	
	function getby_code(){

	$this->data['products']= $this->transfer_model->getby_code($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['location'],$_REQUEST['code'],$_REQUEST['name'],$_REQUEST['category'], $_REQUEST['company'])->result();
    	
		$this->load->view('product/transfer/search_list',$this->data);
	//print_r($this->data['products']);exit;		
	
	}
	function getby_price(){

	$this->data['products']= $this->transfer_model->getby_price($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['location'],$_REQUEST['code'],$_REQUEST['name'],$_REQUEST['category'], $_REQUEST['company'])->result();	
		$this->load->view('product/transfer/search_list',$this->data);
	//print_r($this->data['products']);exit;		
	
	}
	
	function getby_name(){
	$this->data['products']= $this->transfer_model->getby_name($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['location'],$_REQUEST['name'], $_REQUEST['code'], $_REQUEST['category'], $_REQUEST['company'] )->result();	
	//print_r($this->data['products']);exit;	
		$this->load->view('product/transfer/search_list',$this->data);
	}
	
	function getby_category(){
		
	$this->data['products']= $this->transfer_model->getby_category($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['location'],$_REQUEST['category'],$_REQUEST['code'],$_REQUEST['name'],$_REQUEST['company'] )->result();	
    $this->load->view('product/transfer/search_list',$this->data);
	//print_r($this->data['products']);exit;	
		
	
	}

function getby_company(){
		
	$this->data['products']= $this->transfer_model->getby_company($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['location'],$_REQUEST['company'],$_REQUEST['category'],$_REQUEST['code'],$_REQUEST['name'])->result();	
		$this->load->view('product/transfer/search_list',$this->data);
	//print_r($this->data['products']);exit;	
		
	
	}
function getby_location(){
		
	$this->data['products']= $this->transfer_model->getby_location($_REQUEST['price1'],$_REQUEST['price2'],$_REQUEST['company'],$_REQUEST['location'],$_REQUEST['category'],$_REQUEST['code'],$_REQUEST['name'])->result();	
	$this->load->view('product/transfer/search_list2',$this->data);
	
	//print_r($this->data['products']);exit;	
		
	
	}	
	public function view_stock_onhand()
{
$data['company']=$this->transfer_model->get_all_company();
$data['category']=$this->transfer_model->get_all_category();
$data['branch']=$this->transfer_model->get_all_branch();
$data['all_product']=$this->transfer_model->get_all_product();
 $meta['page_title'] = " Stock On-Hand";
$this->load->view('common/header',$meta);
$this->load->view('product/transfer/test',$data);
$this->load->view('common/footer');

}	
public function approved_transfer($id)
{ 
$this->data['transfer']=$this->transfer_model->get_pending_transfer($id);
//$source=$transfer->source_branch;
//$destination=$transfer->destination_branch;

$this->data['transfer_product']=$this->transfer_model->get_pending_transfer_product($id);
 $meta['page_title'] = "Approved Transfer";
$this->load->view('common/header',$meta);
$this->load->view('product/transfer/approved_transfer',$this->data);
$this->load->view('common/footer');

}
public function transfer_approved($id)
{
$this->data['transfer']=$this->transfer_model->get_pending_transfer($id);
$this->data['transfer_product']=$this->transfer_model->get_pending_transfer_product($id);
$count=$_REQUEST['count_all'];
$item_name=$_REQUEST['item_name'];
$code=$this->input->post('item_code');
$transfer_qty=$_REQUEST['transfer_quantity'];
$avail_qty=$_REQUEST['available_quantity'];
$required_qty=$_REQUEST['required_quantity'];
$product_id=$_REQUEST['product_id'];
$transfer_product=$_REQUEST['transfer_product'];
for($i=0;$i<$count;$i++)
{
if($transfer_qty[$i]=="")
{


}
else
{
$id_product=$product_id[$i];
$transfer_product_id=$transfer_product[$i];
$req_qty=$required_qty[$i];
$avaiable_qty=$avail_qty[$i];
$transfer=$transfer_qty[$i];
$this->transfer_model->update_transfer_products($transfer,$transfer_product_id,$id_product,$id);
$this->transfer_model->update_branch_product($transfer,$id_product,$this->data['transfer']->source_branch,$this->data['transfer']->destination_branch);
$this->transfer_model->update_approved_transfer($id);
}

}
$transfer_detail=$this->transfer_model->gets_transfer_detail($id);
if(!empty ($transfer_detail))
{
$trans_id['id']=$this->transfer_model->get_max_transfer()->row();
$company['profile']=$this->transfer_model->get_invoice()->row();
$invoice_code=$company['profile']->invoice_code;
$id=$this->session->userdata['user_id'];
$user['detail']=$this->transfer_model->get_user($id)->row();
$branch_id=$user['detail']->company;
$branch['code']=$this->transfer_model->get_branch_code($branch_id)->row();
$branch_code=$branch['code']->code;
$max_id=$trans_id['id']->id+1;	
$transfer_code='TRANS'.'/'.$invoice_code.'/'.$branch_code.'/'.$max_id;
$transfer_det=$this->transfer_model->get_previous_transfer($id);
 $data1=array('source_branch'=>$transfer_det->source_branch,
		               'destination_branch'=>$transfer_det->destination_branch,
					   'date'             =>$transfer_det->date,
					   'transfer_no'      =>$transfer_code,
					   'source_branch_name'=>$transfer_det->source_branch_name,
					    'destination_branch_name'=>$transfer_det->destination_branch_name,
						'status'=>'Pending',
					   );
$latest_id=$this->transfer_model->insert_transfer($data1);
foreach ($transfer_detail as $detail)
{
$data2['quantity']=intval($detail->quantity)-intval($detail->received_quantity);
$data2['transfer_id']=$latest_id;
$data2['product_code']=$detail->product_code;
$data2['id_product']=$detail->id_product;
$data2['source_branch']=$detail->source_branch;
$data2['destination_branch']=$detail->destination_branch;

$this->transfer_model->create_new_transfer($data2);
}
redirect("product/transfer/view_approved_transfer");
}

else
{
redirect("product/transfer/view_transfer");
}

}


	  
}
?>